from time import sleep

from playwright.sync_api import Page

from service.ResDataService import get_end_data
from service.StatusEnum import StatusEnum
from tools.Result import success_result
from tools.HtmlPageService import get_selector_text
response_data = {}
def handle_response(response):
    """响应处理器（带数据收集功能）"""
    global response_data
    # 记录所有响应URL
    print(response.url)
    if "bidding/detail" in response.url:
        response_data = response.json()




def get_html_detail(page: Page,detail_url:str):
    global response_data
    # 清空历史数据
    response_data = {}
    page.on("response", handle_response)
    page.goto(detail_url, wait_until="networkidle",
              timeout=25000)


    sleep(5)

    return get_result(response_data)


def get_result(data):
    start_price = data.get("biddingOrder", {}).get("openPrice","")
    status = data.get("biddingOrder", {}).get("status","")
    print(start_price)
    return success_result("获取成功", get_end_data(start_price, "0", StatusEnum.get_tjyl_status_info(show_status=status)))
